<?php

require_once("../../connection.php");

require_once('../../autor_message/dao/autor_message_dao.php');
require_once('../../autor_message/to/autor_message_to.php');


class Mysql_autor_message_dao implements Autor_message_dao{

	public function create($autor_message_to) {
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($autor_message_to->get_from_autor_login());
			$to_autor_login = mysql_real_escape_string($autor_message_to->get_to_autor_login());
			$title = mysql_real_escape_string($autor_message_to->get_title());
			$content = mysql_real_escape_string($autor_message_to->get_content());
			$date = mysql_real_escape_string($autor_message_to->get_date());
			$state = mysql_real_escape_string($autor_message_to->get_state());
			
			$result = mysql_query("insert into autor_message 
									(from_autor_login, to_autor_login, title, content, date, state) 
									values 
									('$from_autor_login', '$to_autor_login', '$title',
									'$content', '$date', '$state')", $link);			
			
			if (!$result || (mysql_affected_rows($link) != 1) || 
					(($number = mysql_insert_id($link)) == 0)) 
					throw new Exception(NOT_ROW_INSERTED);
			
			Connection::singleton()->transaction_commit($link);
			
			$autor_message_to = new Autor_message_to($number, $from_autor_login, $to_autor_login, 
														$title,	$content, $date, $state);
			return $autor_message_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function exist($number) {

		try {
			$link = Connection::singleton()->transaction_begin();		
			
			$result = mysql_query("select * from autor_message 
									where number = '$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function find_by_number($number) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from autor_message 
									where number = '$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) 
				throw new Exception(NOT_ROW_FOUND);

			Connection::singleton()->transaction_commit($link);
			
			return mysql_fetch_assoc($result);
		}
		catch (Exception $e) {			
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);		
		}
	}
	
	
	public function update($autor_message_to) {
	
		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$number = $autor_message_to->get_number();
			$state = mysql_real_escape_string($autor_message_to->get_state());
			
			$result = mysql_query("update autor_message 
									set state='$state' 
									where number = '$number'", $link);
							
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_UPDATED);
			
			Connection::singleton()->transaction_commit($link);
			
			return $autor_message_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}		
	}
	
	
	public function remove($number) {	
		try {			
			$link = Connection::singleton()->transaction_begin();		
			
			$result = mysql_query("delete from autor_message
									where number = '$number'", $link);
									
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_DELETED);
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
}
?>